/** PatientDao */
package server.service.patient;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import server.Database;
import domain.patient.Patient;
import domain.user.User;

/**
Talks to the DATABASE about patients
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class PatientDao {
	//private static Database DATABASE;

	/**
	 * Method addPatient.
	 * @param p Patient
	
	 * @return Integer */
	public Integer addPatient(Patient p) {
		return Database.DATABASE.addUser(p, p.getUsername(), p.getPassword());
	}
	
	/**
	 * Method findPatients.
	 * @param s String
	
	 * @return List */
	public List findPatients(String s) {
		List<User> users = Database.DATABASE.users;
		List<Patient> patients = new ArrayList<Patient>();
		for (User u : users) {
			if (u instanceof Patient) {
				Patient patient = (Patient) u;
				if (patient.getNameFirst().toLowerCase().contains(
						s.toLowerCase()) || 
						patient.getNameMiddle().toLowerCase().contains(
								s.toLowerCase())  ||
						patient.getNameLast().toLowerCase().contains(
								s.toLowerCase())) {
					patients.add(patient);
				}
			}
		}
		return patients;
	}

	/**
	 * Method getPatient.
	 * @param id Integer
	
	 * @return Patient */
	public Patient getPatient(Integer id) {
		for (User u : Database.DATABASE.users) {
			if (u instanceof Patient && u.getUserId() == id) return (Patient) u;
		}
		return null;
	}

	/**
	 * Method modifyNameFirst.
	 * @param id Integer
	 * @param nameFirst String
	 */
	public void modifyNameFirst(Integer id, String nameFirst) {
		Patient patient = getPatient(id);
		patient.setNameFirst(nameFirst);
	}

	/**
	 * Method modifyNameMiddle.
	 * @param id Integer
	 * @param nameMiddle String
	 */
	public void modifyNameMiddle(Integer id, String nameMiddle) {
		Patient patient = getPatient(id);
		patient.setNameMiddle(nameMiddle);
	}

	/**
	 * Method modifyNameLast.
	 * @param id Integer
	 * @param nameLast String
	 */
	public void modifyNameLast(Integer id, String nameLast) {
		Patient patient = getPatient(id);
		patient.setNameLast(nameLast);
	}

	/**
	 * Method modifyAddress.
	 * @param id Integer
	 * @param address String
	 */
	public void modifyAddress(Integer id, String address) {
		Patient patient = getPatient(id);
		patient.setAddress(address);
	}

	/**
	 * Method modifyEmail.
	 * @param id Integer
	 * @param email String
	 */
	public void modifyEmail(Integer id, String email) {
		Patient patient = getPatient(id);
		patient.setEmail(email);
	}

	/**
	 * Method modifyDOB.
	 * @param id Integer
	 * @param date Date
	 */
	public void modifyDOB(Integer id, Date date) {
		Patient patient = getPatient(id);
		patient.setDateOfBirth(date);
	}

	/**
	 * Method modifyAllergies.
	 * @param id Integer
	 * @param allergies String
	 */
	public void modifyAllergies(Integer id, String allergies) {
		Patient patient = getPatient(id);
		patient.setAllergies(allergies);
	}

	/**
	 * Method modifyInsurance.
	 * @param id Integer
	 * @param insurance String
	 */
	public void modifyInsurance(Integer id, String insurance) {
		Patient patient = getPatient(id);
		patient.setInsurance(insurance);
	}

	/**
	 * Method getAllPatients.
	
	 * @return Patient[] */
	public List<Patient> getAllPatients() {
		ArrayList<Patient> patients = new ArrayList<Patient>();
		for (User u : Database.DATABASE.users) {
			if (u instanceof Patient) {
				patients.add((Patient) u);
			}
		}
		return patients;
	}
	
	/**
	 * super's toString
	
	 * @return super's toString */
	public String toString() {
		return super.toString();
	}
}
